#include "cm_shareitem.h"

CM_ShareItem::CM_ShareItem(void)
{
}

CM_ShareItem::~CM_ShareItem(void)
{
}

int 
CM_ShareItem::saveShares(int collectionId, WMI_Result<WMI_Share>& shares)
{
    try {
        vector<WMI_Share*> shareVector = shares.getObjects();
        sqlite3_connection con(DEFAULT_DATABASE_NAME.c_str());
		sqlite3_transaction trans(con);
		{
            for(vector<WMI_Share*>::const_iterator it = shareVector.begin(); it != shareVector.end(); it++ )
            {
                sqlite3_command cmd(con, INSERT_SHARE_SCRIPT);
                cmd.bind(1,collectionId);
                cmd.bind(2,(*it)->getAccessMask());
                cmd.bind(3,(*it)->getAllowMaximum());
                cmd.bind(4,(*it)->getCaption());
                cmd.bind(5,(*it)->getDescription());
                cmd.bind(6,(*it)->getInstallDate().GetTime());
                cmd.bind(7,(*it)->getMaximumAllowed());
                cmd.bind(8,(*it)->getName());
                cmd.bind(9,(*it)->getPath());
                cmd.bind(10,(*it)->getStatus());
                cmd.bind(11,(*it)->getType());

			    cmd.executenonquery();
            }		
		}

		trans.commit();
		con.close();
    }
    catch(exception &ex) {
        cerr << "Exception Occured: saveShares(...) " << ex.what() << endl;
        return 0;
    }
    
    return 1;
}

int 
CM_ShareItem::getShares(int collectionId, WMI_Result<WMI_Share>& shares)
{
    try {
        sqlite3_connection con(DEFAULT_DATABASE_NAME.c_str());
        {           
            sqlite3_command cmd(con, SELECT_SHARE_SCRIPT);
            cmd.bind(1, collectionId);
            sqlite3_reader reader = cmd.executereader();

            vector<WMI_Share*>& shareVector = shares.getObjects();

            WMI_Share* share;
			wstring *aux, *aux2, *aux3, *aux4, *aux5, *aux6;

            while(reader.read())
            {	
                share = new WMI_Share();
                share->setAccessMask(reader.getint(2));
                share->setAllowMaximum(reader.getint(3));
				aux = new wstring(reader.getstring16(4));
                share->setCaption(*aux);
                aux2 = new wstring(reader.getstring16(5));
                share->setDescription(*aux2);
                share->setInstallDate(CTime(reader.getint64(6)));
                share->setMaximumAllowed(reader.getint(7));
                aux3 = new wstring(reader.getstring16(8));
                share->setName(*aux3);                
                aux4 = new wstring(reader.getstring16(9));
                share->setPath(*aux4);                
                aux5 = new wstring(reader.getstring16(10));
                share->setStatus(*aux5);                
                aux6 = new wstring(reader.getstring16(11));
                share->setType(*aux6);                
                shareVector.push_back(share);
            }             
        }

        con.close();
    }
    catch(exception &ex) {
        cerr << "Exception Occured: " << ex.what() << endl;
        return 0;
    }

    return 1;
}
